package com.kkb.robotcustomerservice.controller;

import com.github.pagehelper.PageInfo;
import com.kkb.robotcustomerservice.entities.KnowledgeBase;
import com.kkb.robotcustomerservice.util.User;
import com.kkb.robotcustomerservice.service.KnowledgeBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;


@Controller
@CrossOrigin
@RequestMapping("/KnowledgeBase")
public class UseKnowledgeBaseController {

    @Autowired
    KnowledgeBaseService knowledgeBaseService;
    /**
     * 模拟商户登录，携带商户数据
     * @param model 可以将此请求携带用户信息
     * @return
     */
    @RequestMapping("/toKnowledgeBase")
    public String toKnowledgeBase(User user,
                                  Model model,
                                  @RequestParam(defaultValue = "1") int page,
                                  @RequestParam(defaultValue = "10") int rows) {

        //在此查询出分页数据一同存储到model中
        PageInfo<KnowledgeBase> pageInfo = knowledgeBaseService.findPage(page, rows, user);
        model.addAttribute("page", pageInfo);
        model.addAttribute("user", user);
        return "knowledgebase";
    }

    /**
     * 跳转到编辑页面
     * @param qId
     * @param model
     * @return
     */
    @RequestMapping("/toAlertUpdate")
    public String toAlertUpdate(User user,@RequestParam int qId,Model model) {
        KnowledgeBase knowledgeBase = knowledgeBaseService.getById(qId);
        model.addAttribute("knowledgeBase", knowledgeBase);
        model.addAttribute("user", user);
        return "alertupdate";
    }

    @RequestMapping(value = "/alertUpdate")
    public String AlertUpdate(KnowledgeBase knowledgeBase){
        knowledgeBaseService.updateById(knowledgeBase);
        return "close";
    }

    @RequestMapping("/close")
    public String close(){
        return "close";
    }

    @RequestMapping("/toAlertAdd")
    public String toAlertAdd(User user,Model model) {
        model.addAttribute("user", user);
        return "alertadd";
    }

    @RequestMapping("/toUploadExcel")
    public String toUploadExcel(User user, Model model) {
        model.addAttribute("user", user);
        return "alertmultiupload";
    }

    @RequestMapping(value = "/alertAdd")
    public String AlertAdd(KnowledgeBase knowledgeBase){
        knowledgeBaseService.insertOne(knowledgeBase);
        return "close";
    }

    /**
     * 批量删除
     * @param user
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequestMapping("/deleteAll")
    public String deleteAll(User user) throws UnsupportedEncodingException {

        knowledgeBaseService.deleteAll(user.getUsername(),user.getSort());

        //中文乱码处理
        String username = URLEncoder.encode(user.getUsername(), "utf-8");
        String sort = URLEncoder.encode(user.getSort(), "utf-8");

        String url = "redirect:/KnowledgeBase/toKnowledgeBase?username="+username+"&sort="+sort;
        return url;
    }


    /**
     * 根据ID删除数据
     * @param user
     * @param qId
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequestMapping("/deleteById")
    public String delete(User user, @RequestParam int qId) throws UnsupportedEncodingException {
        knowledgeBaseService.deleteById(qId);
        //中文乱码处理
        String username = URLEncoder.encode(user.getUsername(), "utf-8");
        String sort = URLEncoder.encode(user.getSort(), "utf-8");

        String url = "redirect:/KnowledgeBase/toKnowledgeBase?username="+username+"&sort="+sort;
        return url;
    }

    @RequestMapping("/toUpdateStatus")
    public String toUpdateStatus(User user,@RequestParam int qId,@RequestParam String page) throws UnsupportedEncodingException {
        KnowledgeBase knowledgeBase = knowledgeBaseService.getById(qId);
        if (knowledgeBase.getStatus().equals("0")){
            knowledgeBase.setStatus("1");
        }else{
            knowledgeBase.setStatus("0");
        }
        knowledgeBaseService.updateById(knowledgeBase);
        String username = URLEncoder.encode(user.getUsername(), "utf-8");
        String sort = URLEncoder.encode(user.getSort(), "utf-8");

        String url = "redirect:/KnowledgeBase/toKnowledgeBase?username="+username+"&sort="+sort+"&page="+page;
        return url;
    }
}
